<?php


namespace app\common\model\tang;

use app\common\impl\Comm;
use app\common\model\CommonBaseModel;

class ElectricityBill extends CommonBaseModel implements Comm
{

    //读取器 缴费日期 pay_date
    public function getPayDateAttr($value,$data)
    {

        return date("Y-m-d h:i:s",$value);
    }


    //读取器 结算日期 settle_date
    public function getSettleDateAttr($value,$data)
    {

        return date("Y-m-d h:i:s",$value);
    }

    //读取器 支付状态：1=未支付 2=已支付
    public function getStatusAttr($value,$data)
    {
        if ($value == 1){
            $value = "未支付";
        }
        if ($value == 2){
            $value = "已支付";
        }
        return $value;
    }

    //读取器 type 类型：1=单一用电   2=费率用电
    public function getTypeAttr($value,$data)
    {
        if ($value == 1){
            $value = "单一用电";
        }
        if ($value == 2){
            $value = "费率用电";
        }
        return $value;
    }

    public function getAdminPageData($param = [], $order = 'id desc',$where=[])
    {
        $model = self::where($where);
        $data = $model->select()->toArray();

        $count = $model->count("id");
        return parent::ajaxResult($data,$count);
//        // TODO: Implement getAdminPageData() method.
//        if (isset($param['merchant_id']) && $param['merchant_id']){
//            $model->where('merchant_id','=',$param['merchant_id']);
//        }
//        if (isset($param['mobile']) && $param['mobile']){
//            $model->where('mobile','like',"%{$param['mobile']}%");
//        }
//        if (isset($param['order_sn']) && $param['order_sn']){
//            $model->where('order_sn','like',"%{$param['order_sn']}%");
//        }
//        $data = $model->with(['merchant'])->order($order)->page(PAGE)->limit(LIMIT)->select()->toArray();
//        $count = $model->count("id");
//        return parent::ajaxResult($data, $count);
    }

    // 退款

    public function Refund($param = [], $order = 'id desc',$where =[]){
        $model = self::where($where);
        if (isset($param['merchant_id']) && $param['merchant_id']){
            $model->where('merchant_id','=',$param['merchant_id']);

        }
        if (isset($param['order_sn']) && $param['order_sn']){
            $model->where('order_sn','like',"%{$param['order_sn']}%");
        }
        if (!empty($param['data'])){
            $data = explode(' - ',$param['data']);
            $model->where('create_time','>=',strtotime($data[0]))->where('create_time','<=',strtotime($data[1]));
        }

        // TODO: Implement getAdminPageData() method.
        $data = $model->with(['merchant','cost'])->order($order)->page(PAGE)->limit(LIMIT)->select()->toArray();
        $count = $model->count("id");

//        if($data){
//            foreach ($data as&$v){
//                if($v['is_admin_create']==0 || $v['is_user']==0){  // 人工创建
//                    $v['fname']  = $v['costType']['cost_name'] ;
//                }else{ // 自主创建
//                    switch ($v['cost_type_id']){
//                        case 1 :
//                            $v['fname'] = '电费' ;
//                            break ;
//                        case 2 :
//                            $v['fname'] = '水费' ;
//                            break ;
//                    }
//                }
//            }
//        }
        return parent::ajaxResult($data, $count);

    }

    public function getAdminPageDatas($param = [], $order = 'id desc',$where=[])
    {
        // TODO: Implement getAdminPageData() method.
        isset($param['id']) && !empty($param['id'])?$where['merchant_id'] =$param['id']:false;
        $data = self::where($where)->with(['merchant'])->order($order)->page(PAGE)->limit(LIMIT)->select()->toArray();
        $count = self::where($where)->count("id");
        return parent::ajaxResult($data, $count);
    }

    public function delById($id)
    {
        // TODO: Implement delById() method.
    }

    public function doAll($data)
    {
        // TODO: Implement doAll() method.
        return parent::doAllData($data);
    }
    public function merchant()
    {
        return $this->hasOne(Merchant::class,'id','merchant_id')->field('id,merchant_sn,name,company');
    }
    public function cost()
    {
        return $this->hasOne(Cost::class,'order_sn','order_sn');
    }
}